#!/usr/bin/env python
# -*- coding:utf-8 -*- 

from flask_restful import Resource, reqparse
from flask import jsonify, request

from controller.userCounselorController import UserCounselorController
from service.userCounselorService import UserCounselorService
from utils import commons
from utils.response_code import RET, error_map_EN
import json


class UserCounselorResource(Resource):

    # get方法
    @classmethod
    def get(cls, user_id=None):
        if user_id:
            kwargs = {
                'user_id': user_id
            }

            res = UserCounselorController.get(**kwargs)
            if res['code'] == RET.OK:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])
            else:
                return jsonify(code=res['code'], message=res['message'], data=res['data'])

        parser = reqparse.RequestParser()
        parser.add_argument('auto_id', location='args', required=False, help='auto_id参数类型不正确或缺失')
        parser.add_argument('user_id', location='args', required=False, help='user_id参数类型不正确或缺失')
        parser.add_argument('name', location='args', required=False, help='name参数类型不正确或缺失')
        parser.add_argument('gender', location='args', required=False, help='gender参数类型不正确或缺失')
        parser.add_argument('mobile', location='args', required=False, help='mobile参数类型不正确或缺失')
        parser.add_argument('email', location='args', required=False, help='email参数类型不正确或缺失')
        parser.add_argument('address', location='args', required=False, help='address参数类型不正确或缺失')
        parser.add_argument('level', location='args', required=False, help='level参数类型不正确或缺失')
        parser.add_argument('workplace', location='args', required=False, help='workplace参数类型不正确或缺失')
        parser.add_argument('tag', location='args', required=False, help='tag参数类型不正确或缺失')
        parser.add_argument('is_recommend', location='args', required=False, help='is_recommend参数类型不正确或缺失')
        parser.add_argument('is_delete', location='args', required=False, help='is_delete参数类型不正确或缺失')
        parser.add_argument('create_time', location='args', required=False, help='create_time参数类型不正确或缺失')
        parser.add_argument('update_time', location='args', required=False, help='update_time参数类型不正确或缺失')
        
        parser.add_argument('Page', location='args', required=False, help='Page参数类型不正确或缺失')
        parser.add_argument('Size', location='args', required=False, help='Size参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = UserCounselorController.get(**kwargs)
        if res['code'] == RET.OK:
            return jsonify(code=res['code'], message=res['message'], data=res['data'], totalPage=res['totalPage'], totalCount=res['totalCount'])
        else:
            return jsonify(code=res['code'], message=res['message'], data=res['data']) 

    
    # delete
    @classmethod
    def delete(cls, user_id=None):
        if user_id:
            kwargs = {
                'user_id': user_id
            }

        else:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='id不能为空')

        res = UserCounselorController.delete(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    # put方法
    @classmethod
    def put(cls, user_id):
        if not user_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('name', location='json', required=False, help='name参数类型不正确或缺失')
        parser.add_argument('mobile', location='json', required=False, help='mobile参数类型不正确或缺失')
        parser.add_argument('email', location='json', required=False, help='email参数类型不正确或缺失')
        parser.add_argument('address', location='json', required=False, help='address参数类型不正确或缺失')
        parser.add_argument('level', location='json', required=False, help='level参数类型不正确或缺失')
        parser.add_argument('workplace', location='json', required=False, help='workplace参数类型不正确或缺失')
        parser.add_argument('tag', location='json', required=False, help='tag参数类型不正确或缺失')
        parser.add_argument('certificate', location='json', required=False, help='certificate参数类型不正确或缺失')
        parser.add_argument('status', location='json', required=False, help='status参数类型不正确或缺失')
        parser.add_argument('gender', location='json', required=False, help='gender参数类型不正确或缺失')
        parser.add_argument('is_recommend', location='json', required=False, help='is_recommend参数类型不正确或缺失')
        parser.add_argument('is_delete', location='json', required=False, help='is_delete参数类型不正确或缺失')


        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['user_id'] = user_id

        res = UserCounselorController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    
    # add&login
    @classmethod
    def post(cls):
        try:
            # 登录接口
            if request.path.endswith('/login'):
                parser = reqparse.RequestParser()
                parser.add_argument('account', type=str, required=True, 
                                  help='account参数不能为空')
                parser.add_argument('password', type=str, required=True, 
                                  help='password参数不能为空')
                
                args = parser.parse_args()
                result = UserCounselorService.login(**args)
                return jsonify(result)
            
            # 原有的添加接口
            parser = reqparse.RequestParser()
            parser.add_argument('UserCounselorList', type=str, location='form', required=False, help='UserCounselorList参数类型不正确或缺失')

            kwargs = parser.parse_args()
            kwargs = commons.put_remove_none(**kwargs)

            if kwargs.get('UserCounselorList'):
                kwargs['UserCounselorList'] = json.loads(kwargs['UserCounselorList'])
                for data in kwargs['UserCounselorList']:
                    for key in ['gender', 'tag', 'is_recommend', 'is_delete']:
                        data.pop(key, None)
                res = UserCounselorController.add_list(**kwargs)

            else:
                parser.add_argument('name', location='json', required=False, help='name参数类型不正确或缺失')
                parser.add_argument('mobile', location='json', required=True, help='mobile参数类型不正确或缺失')
                parser.add_argument('email', location='json', required=False, help='email参数类型不正确或缺失')
                parser.add_argument('address', location='json', required=False, help='address参数类型不正确或缺失')
                parser.add_argument('level', location='json', required=False, help='level参数类型不正确或缺失')
                parser.add_argument('gender', location='json', required=True, help='gender参数类型不正确或缺失')
                parser.add_argument('tag', location='json', required=True, help='tag参数类型不正确或缺失')
                parser.add_argument('certificate', location='json', required=True, help='certificate参数类型不正确或缺失')
                parser.add_argument('workplace', location='json', required=False, help='workplace参数类型不正确或缺失')
                parser.add_argument('create_time', location='json', required=False, help='create_time参数类型不正确或缺失')
                parser.add_argument('update_time', location='json', required=False, help='update_time参数类型不正确或缺失')
                
                kwargs = parser.parse_args()
                kwargs = commons.put_remove_none(**kwargs)

                res = UserCounselorController.add(**kwargs)

            return jsonify(code=res['code'], message=res['message'], data=res['data'])

        except Exception as e:
            print(f"Error: {str(e)}")
            return jsonify({
                "code": "5000",
                "message": "服务器内部错误",
                "error": str(e)
            })
